//
//  GYMineLikeBuyInfoVC.m
//  GYDemo
//
//  Created by mac on 2018/3/27.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYMineLikeBuyInfoVC.h"
#import "Head.h"

@interface GYMineLikeBuyInfoVC ()
{
    NSInteger currentPage;
}
@property (nonatomic,strong) GYMineLikeBuyInfoViewModel *likeBuyInfoviewModel;
@end

@implementation GYMineLikeBuyInfoVC
#pragma mark - getter and setter
-(GYMineLikeBuyInfoViewModel *)likeBuyInfoviewModel
{
    if (!_likeBuyInfoviewModel) {
        _likeBuyInfoviewModel = [GYMineLikeBuyInfoViewModel new];
    }
    return _likeBuyInfoviewModel;
}
#pragma mark - event
#pragma mark - method
-(void)initilization
{
    self.navigationItem.title = @"我收藏的求购信息";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self setNavgationTheme:1];
    [self.view addSubview:self.baseTable];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.height = self.view.height - 64;
    @WeakSelf;
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        currentPage = 1;
        [selfp reloadData];
    }];
    self.baseTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        currentPage += 1;
        [selfp reloadData];
    }];
    [self viewModelBand];
    //    [self.baseTable.mj_header beginRefreshing];
    [self reloadData];
}
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.likeBuyInfoviewModel;
    observation.keyPath = @"datas";
    observation.handler = ^(NSString *keyPath) {
        [selfp reloadView];
    };
    [self registObservation:observation];
}
-(void)reloadView
{
    [self.baseTable reloadData];
}
-(void)reloadData
{
    NSMutableDictionary *para = [NSMutableDictionary new];
    NSString *page = [NSString stringWithFormat:@"%ld",currentPage];
    NSString *pageSize = @"10";
    [para setObject:page forKey:@"page"];
    [para setObject:pageSize forKey:@"pagesize"];
    [self.likeBuyInfoviewModel requestDatas:para];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.likeBuyInfoviewModel.datas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"CellID";
    GYMineLikeBuyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[GYMineLikeBuyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    @WeakSelf
    [cell.deleteBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        [selfp tableView:tableView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath];
    }];
    GYMineLikeBuyInfoModel *model = self.likeBuyInfoviewModel.datas[indexPath.row];
    cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [GYMineLikeBuyCell finalHeight];
    return height;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        //删除
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定删除吗？" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
        @WeakSelf
        [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
            if (1 == buttonIndex) {
                GYMineLikeBuyInfoModel *model = selfp.likeBuyInfoviewModel.datas[indexPath.row];
                [selfp.likeBuyInfoviewModel requestDelete:model];
            }
        }];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GYMineLikeBuyInfoModel *lmodel = self.likeBuyInfoviewModel.datas[indexPath.row];
    GYMineBuyInfoModel *model = [GYMineBuyInfoModel yy_modelWithJSON:[lmodel yy_modelToJSONString]];
    model.id = lmodel.proid;
    GYMineBuyInfoDetailVC *detail = [GYMineBuyInfoDetailVC new];
    detail.canNotEdit = YES;
    detail.buyInfoModel = model;
    [self.navigationController pushViewController:detail animated:YES];
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
